package com.controllor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.HumanDao;
import com.dao.ManagerDao;
import com.dao.OrdinaryEmployeesDao;
import com.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.util.JSON_to_layui;

/**
 * Servlet implementation class HumanServlet
 */
@WebServlet("/HumanServlet")
public class HumanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public HumanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		User USER = (User) session.getAttribute("user");
		String userid = USER.getUserid();
//*****“lqq”临时测试，记得改为“userid”*********
//		String userid = "test";
//		session.setAttribute("userid", userid);//临时用，本该在登录servlet进行session.setAttribute（）
		String button = request.getParameter("button");
//		String button ="4";
		
		//分类显示全部申请
		if("1".equals(button)) {
			System.out.println("Human..button"+button);
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			String status = request.getParameter("condition");	//查询用
			String applytype = request.getParameter("applytype");
			System.out.println("status:"+status);
			
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			String json = mapper.writeValueAsString(HumanDao.getAllApply(status,applytype, page, limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,HumanDao.count,"");
			}
			response.getWriter().print(json);
		}
		//处理申请
		else if("2".equals(button)) {
			System.out.println("Human..button"+button);
			boolean action = false;
			PrintWriter out = response.getWriter();
			String status = request.getParameter("status");		//同意或拒绝
			int applyid = Integer.parseInt(request.getParameter("applyid"));
			String operater = request.getParameter("operater");
			String applytype = request.getParameter("applytype");
			System.out.println("参数："+status+"  "+applyid+"  "+operater+" "+applytype);
			if("1".equals(status)) {
				if("inputOvertime".equals(applytype)) {
					int overtime = Integer.parseInt(request.getParameter("overtime"));
					int weekovertime = Integer.parseInt(request.getParameter("weekovertime"));
					int festovertime = Integer.parseInt(request.getParameter("festovertime"));
					action = HumanDao.doApply(status,userid, operater, applyid,applytype,overtime,weekovertime,festovertime,0,0,0,0,0);
				}else if("inputPunishing".equals(applytype)) {
					int leavecount = Integer.parseInt(request.getParameter("leavecount"));
					int absentcount = Integer.parseInt(request.getParameter("absentcount"));
					action = HumanDao.doApply(status,userid, operater, applyid,applytype,0,0,0,leavecount,absentcount,0,0,0);
				}else if("inputCommission".equals(applytype)) {
					int commission = Integer.parseInt(request.getParameter("commission"));
					action = HumanDao.doApply(status,userid, operater, applyid,applytype,0,0,0,0,0,commission,0,0);
				}else if("inputBonus".equals(applytype)) {
					int festivalbonus = Integer.parseInt(request.getParameter("festivalbonus"));
					int yearbonus = Integer.parseInt(request.getParameter("yearbonus"));
					action = HumanDao.doApply(status,userid, operater, applyid,applytype,0,0,0,0,0,0,festivalbonus,yearbonus);
				}
				if(action) {
					out.println("同意成功！");
				}else {
					out.println("同意失败！");
				}
			}else {
				action = HumanDao.doApply(status,userid, "", applyid,"",0,0,0,0,0,0,0,0);
				if(action) {
					out.println("拒绝成功！");
				}else {
					out.println("拒绝失败！");
				}
			}
		}
		//获取全员工所有 User 表的 薪资信息or基本信息
		else if("3".equals(button)) {
			System.out.println("Human..button"+button);
			String conditionValue = request.getParameter("conditionValue");
			String condition = request.getParameter("condition");
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			
			System.out.println("conditionValue"+conditionValue);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(HumanDao.getUserInfo(condition,conditionValue,page,limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,HumanDao.count,"");
			}
			response.getWriter().print(json);
		}
		//获取部门列表or角色列表
		else if("4".equals(button)) {
			System.out.println("Human..button"+button);
			String getType = request.getParameter("getType");
//			String getType = "dept";
//			System.out.println("getType"+getType);
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(HumanDao.getDeptRole(getType));
//			System.out.println("json:"+json);
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,HumanDao.count,"");
			}
			response.getWriter().print(json);
		}
		//人力部 可修改全员工的 所属部门、职位、基本薪资、加班标准、请假标准、旷工薪资
		else if("5".equals(button)) {
			System.out.println("Human..button"+button);
			String type = request.getParameter("type");
			String object = request.getParameter("object");
			String newvalue = request.getParameter("newvalue");
			System.out.println("type"+type+"object"+object+"newvalue"+newvalue);
			
			PrintWriter out = response.getWriter();
			if(OrdinaryEmployeesDao.updateUserInfo(object, type, newvalue)) {
				out.println("修改成功！");
			}else {
				out.println("修改失败！");
			}
		}
		//由部门id与职位id 得各项薪资标准
		else if("6".equals(button)) {
			System.out.println("Human..button"+button);
			int deptid = Integer.parseInt(request.getParameter("deptid"));
			int roleid = Integer.parseInt(request.getParameter("roleid"));
//			System.out.println("deptid"+deptid+"roleid"+roleid);
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(HumanDao.getStandard(deptid, roleid));
//			System.out.println("json:"+json);
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,HumanDao.count,"");
			}
			response.getWriter().print(json);
		}
		//添加新职工
		else if("7".equals(button)) {
			System.out.println("Human..button"+button);
			
			String newuserid = request.getParameter("userid");
			String deptid = request.getParameter("deptid");
			String roleid = request.getParameter("roleid");
			String pwd = request.getParameter("pwd");
			String status = request.getParameter("status");
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			int age = Integer.parseInt(request.getParameter("age"));
			String phone = request.getParameter("phone");
			float basewages = Float.parseFloat(request.getParameter("basewages"));
			float overtimestandard = Float.parseFloat(request.getParameter("overtimestandard"));
			float leavestandard = Float.parseFloat(request.getParameter("leavestandard"));
			float absentstandard = Float.parseFloat(request.getParameter("absentstandard"));
			System.out.println("userid"+newuserid);
			User user = new User(newuserid,deptid,roleid,pwd,status,name,sex,age,phone,basewages,overtimestandard,leavestandard,absentstandard,"0");
			PrintWriter out = response.getWriter();
			if(HumanDao.register(user)) {
				out.print("添加成功！");
			}else {
				out.print("添加失败！");
			}
		}
		//删除职工信息
		else if("8".equals(button)) {
			PrintWriter out = response.getWriter();
			System.out.println("Human..button"+button);
			String operater = request.getParameter("userid");
			if(HumanDao.deleteUser(operater)) {
				out.print("成功！");
			}else {
				out.print("失败！");
			}
		}
		//财务部薪资发放
		else if("9".equals(button)) {
			PrintWriter out = response.getWriter();
			System.out.println("Human..button"+button);
			String operater = request.getParameter("operater");
			if(HumanDao.grantSalary(operater)) {
				out.print("true");
			}else {
				out.print("false");
			}
		}
		//薪资统计表获取数据
		else if("10".equals(button)) {
			System.out.println("Human..button"+button);
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			mapper.writeValue(response.getWriter(), HumanDao.getStatistic());
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
